$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'erp/customerinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'customerInfoId', name: 'customerInfoId', index: 'customer_info_id', width: 50, key: true ,hidden:true},
			{ label: '客户编码', name: 'customerCode', index: 'customer_code', width: 80 }, 			
			{ label: '客户名称', name: 'customerName', index: 'customer_name', width: 80 }, 			
			{ label: '客户简称', name: 'customerShortName', index: 'customer_short_name', width: 80 }, 			
			{ label: '主管海关', name: 'mainCustom', index: 'main_custom', width: 80 }, 			
			{ label: '客户类别', name: 'customerType', index: 'customer_type', width: 80 }, 			
			{ label: '业务部联系人', name: 'businessLinkman', index: 'business_linkman', width: 80 }, 			
			{ label: '财务部联系人', name: 'financeLinkman', index: 'finance_linkman', width: 80 }, 			
			{ label: '业务部电话', name: 'businessPhone', index: 'business_phone', width: 80 }, 			
			{ label: '财务部电话', name: 'financePhone', index: 'finance_phone', width: 80 }, 			
			{ label: '传真号码', name: 'faxNumber', index: 'fax_number', width: 80 }, 			
			{ label: '电子邮件', name: 'emailAddress', index: 'email_address', width: 80 }, 			
			{ label: '业务员', name: 'businessMan', index: 'business_man', width: 80 }, 			
			{ label: '付款期', name: 'paymentPeriod', index: 'payment_period', width: 80 }, 			
			{ label: '折扣率', name: 'discountRate', index: 'discount_rate', width: 80 }, 			
			{ label: '付款方式', name: 'paymentMethod', index: 'payment_method', width: 80 }, 			
			{ label: '默认货币', name: 'defaultCurrency', index: 'default_currency', width: 80 }, 			
			{ label: '税率（%）', name: 'taxRate', index: 'tax_rate', width: 80 }, 			
			{ label: '客户信贷额', name: 'customerCredit', index: 'customer_credit', width: 80 }, 			
			{ label: '月结期间（1：上月0：本月）', name: 'monthlyPeriod', index: 'monthly_period', width: 80 }, 			
			{ label: '月结开始日期', name: 'payStart', index: 'pay_start', width: 80 }, 			
			{ label: '月结结束日期', name: 'payEnd', index: 'pay_end', width: 80 }, 			
			{ label: '送货地区', name: 'deliveryArea', index: 'delivery_area', width: 80 }, 			
			{ label: '邮政编号', name: 'postCode', index: 'post_code', width: 80 }, 			
			{ label: '通信地址', name: 'mailingAddress', index: 'mailing_address', width: 80 }, 			
			{ label: '送货地址', name: 'deliveryAddress', index: 'delivery_address', width: 80 }, 			
			{ label: '重要说明', name: 'importantExplain', index: 'important_explain', width: 80 }, 			
			{ label: '状态（1：有效0无效）', name: 'status', index: 'status', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		customerInfo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.customerInfo = {};
		},
		update: function (event) {
			var customerInfoId = getSelectedRow();
			if(customerInfoId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(customerInfoId)
		},
		saveOrUpdate: function (event) {
			debugger;
			console.log(vm.customerInfo);
			var url = vm.customerInfo.customerInfoId == null ? "erp/customerinfo/save" : "erp/customerinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.customerInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var customerInfoIds = getSelectedRows();
			if(customerInfoIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "erp/customerinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(customerInfoIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(customerInfoId){
			$.get(baseURL + "erp/customerinfo/info/"+customerInfoId, function(r){
                vm.customerInfo = r.customerInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});